package troopmanager.mooresr.us;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class GoogleMaps {
	static private GoogleMaps instance = null;
	
	private String startLocation;
	private String endLocation;
	private Integer distance;
	private String startAddress;
	private String endAddress;
	
	private GoogleMaps() {
		startLocation = null;
		endLocation = null;
	}
	
	static public GoogleMaps getInstance() {
		if (instance == null) {
			instance = new GoogleMaps();
		}
		
		return instance;
	}
	
	public void setStartLocation(String location) {
		startLocation = location;
	}
	
	public void setEndLocation(String location) {
		endLocation = location;
	}
	
	public void submit() throws Exception {
		String url = "http://maps.google.com/maps/api/directions/xml?origin=";
		// 8021+Arum+Pl,Antelope,CA&destination=8000+Foothills+Blvd,Roseville,CA&sensor=false"
		url = url + normalizeLocation(startLocation);
		url = url + "&destination=";
		url = url + normalizeLocation(endLocation);
		url = url + "&sensor=false";
		
		
        URL googleMaps = new URL(url);
        URLConnection mapConnection = googleMaps.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                mapConnection.getInputStream()));
        String inputLine;

        boolean inStep = false;
        boolean inDistance = true;
        
        while ((inputLine = in.readLine()) != null) {
        
        	// Begin code to ignore everything inside a <step> tag
        	if (inStep) {
        		if (inputLine.contains("</step>")) {
        			inStep = false;
        		}
        		continue;
        	}
        	
        	if (inputLine.contains("<step>")) {
        		inStep = true;
        		continue;
        	}
        	// End code to ignore everything inside a <step> tag
        	
        	
            if (inputLine.contains("<status>")) {
            	int start = inputLine.indexOf("<status>") + 8;
            	int end = inputLine.indexOf("</status>") - 1;
            	String status = inputLine.substring(start, end+1);
            	if (! status.equals("OK")) {
            		throw new Exception();
            	}
            }
            
            if (inputLine.contains("<distance>")) {
            	inDistance = true;
            	continue;
            }
            
            if (inputLine.contains("</distance>")) {
            	inDistance = false;
            	continue;
            }
            
            if ((inDistance) && (inputLine.contains("<value>"))) {
            	int start = inputLine.indexOf("<value>") + 7;
            	int end = inputLine.indexOf("</value>") - 1;
            	String value = inputLine.substring(start, end+1);
            	int meters = Integer.parseInt(value);
            	float miles = (float)(meters) / (float) 1609.344;
            	distance = Math.round(miles);
            }
            
            if (inputLine.contains("<start_address>")) {
            	int start = inputLine.indexOf("<start_address>") + 15;
            	int end = inputLine.indexOf("</start_address>") - 1;
            	startAddress = inputLine.substring(start, end+1);	
            }
            
            if (inputLine.contains("<end_address>")) {
            	int start = inputLine.indexOf("<end_address>") + 13;
            	int end = inputLine.indexOf("</end_address>") - 1;
            	endAddress = inputLine.substring(start, end+1);	
            }
            
        }

        in.close();
	
	}
	
	private String normalizeLocation(String location) {
		return location.replaceAll(" ", "+");
		
	}
	
	public Integer getDistance() {
		return distance;
	}
	
	public String getStartAddress() {
		return startAddress;
	}
	
	public String getEndAddress() {
		return endAddress;	
	}
}
